package com.erp.freight.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.freight.domain.KjxStoreProductInfo;
import com.erp.freight.domain.bo.KjxStoreProductInfoBo;
import com.erp.freight.domain.vo.KjxStoreProductInfoVo;
import com.erp.freight.mapper.KjxStoreProductInfoMapper;
import com.erp.freight.service.IKjxStoreProductInfoService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 熊迪库存商品Service业务层处理
 *
 * @author erp
 * @date 2024-04-07
 */
@RequiredArgsConstructor
@Service
public class KjxStoreProductInfoServiceImpl implements IKjxStoreProductInfoService {

    private final KjxStoreProductInfoMapper baseMapper;

    /**
     * 查询熊迪库存商品
     */
    @Override
    public KjxStoreProductInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询熊迪库存商品列表
     */
    @Override
    public TableDataInfo<KjxStoreProductInfoVo> queryPageList(KjxStoreProductInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KjxStoreProductInfo> lqw = buildQueryWrapper(bo);
        Page<KjxStoreProductInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询熊迪库存商品列表
     */
    @Override
    public List<KjxStoreProductInfoVo> queryList(KjxStoreProductInfoBo bo) {
        LambdaQueryWrapper<KjxStoreProductInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KjxStoreProductInfo> buildQueryWrapper(KjxStoreProductInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KjxStoreProductInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseId()), KjxStoreProductInfo::getWarehouseId, bo.getWarehouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), KjxStoreProductInfo::getSkuCode, bo.getSkuCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCount()), KjxStoreProductInfo::getCount, bo.getCount());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), KjxStoreProductInfo::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增熊迪库存商品
     */
    @Override
    public Boolean insertByBo(KjxStoreProductInfoBo bo) {
        KjxStoreProductInfo add = BeanUtil.toBean(bo, KjxStoreProductInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改熊迪库存商品
     */
    @Override
    public Boolean updateByBo(KjxStoreProductInfoBo bo) {
        KjxStoreProductInfo update = BeanUtil.toBean(bo, KjxStoreProductInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KjxStoreProductInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除熊迪库存商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
